package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
@Transactional
public class ApplicationImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        //     设置新增申请单的状态是"已发起"
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        //  由于业务原因,没有修改,根据业务,可以实现撤销
        // 因为即将要生成的审批单需要设置申请单的id,但申请单的id无法通过任何条件查询获取,所以需要给ApplicationMapper.xml中对应的insert语句进行配置
        // useGeneratedKeys="true" keyProperty="id"
        // 这两句代码的作用是让JDBC为
        // 当前的Application对象自动生成主键
        // 并且将自增主键赋值给application对象的id字段
        applicationMapper.insert(application);

        // 调用审核模块的业务层方法,新增当前申请单对应的审批单
        auditService.insertAudit(application);

    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        // 根据前段传过来的查询条件,查到所有复合条件的申请单
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        for (int i = 0; i < list.size(); i++) {
            /*
             * 在后端查询的字段中,并没有ApplicationVO的  auditUserIdList (审批人id集合) 和 auditUsernameList(审批人昵称集合)字段
             * */
            ApplicationVO applicationVO = list.get(i);
            //     assign 拼接;组装
            //     车辆申请单需要审批人姓名与id
            //     需要再这个自定义方法总查到当前循环到的申请单对应的所有审批人信息并且存入 ApplicationVO中
            //     为ApplicationVO中的 auditUserIdList (审批人id集合) 和 auditUsernameList(审批人昵称集合)字段赋值
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        Application application = new Application();
        application.setId(id);
        // 修改申请单状态为"撤销"
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.update(application);
    //     删除此撒很轻对应所有的审批单数据
        auditMapper.deleteByApplicationiId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        //更细申请单的数据
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLICATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //更新车辆数据
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        //更细申请单的数据
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        // 设置申请单的状态为"工单结束"
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //更新车辆数据
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1"); //设置车辆状态为"空闲"
        vehicleMapper.update(vehicle);
    }

    // 处理当前申请单的审批人相关的信息
    // 处理auditUserIdList (审批人id集合) 和 auditUsernameList(审批人昵称集合)字段
    private void assignAuditUserList(ApplicationVO applicationVO) {
        //这两个集合对应的审批人的姓名与审批人的Id
        List<String> auditUsernameList  = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //根据传入车辆申请单,查询对应的审批单数据(一个申请单可以对应多个审批单)
        List<AuditVO> auditVOList =auditMapper.selectAuditByApplicationId(applicationVO.getId());
        for (int i = 0; i < auditVOList.size(); i++) {
            AuditVO auditVO = auditVOList.get(i);
            Long id= auditVO.getAuditUserId();
            // 存入审批人的UserId 对应(AuditUserId)字段
            auditUserIdList.add(id);
            // 审批人的姓名 userMapper.selectById(id) 返回值是UserVO
            String username = userMapper.selectById(id).getUsername();
            auditUsernameList.add(username);
        }
        // 需要准备一个工具,帮我们进行值与值的拼接
        StringJoiner stringJoiner= new StringJoiner(",");
        for (String username : auditUsernameList) {
            stringJoiner.add(username);
        }
        applicationVO.setAuditUsernameList(stringJoiner.toString());
        applicationVO.setAuditUserIdList(auditUserIdList);
    }
}
